package com.success.application.ground;

import android.annotation.SuppressLint;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.success.application.AppConfig;
import com.success.application.api.YuanbaoApi;
import com.success.application.base.BaseRecyclerViewAdapter;
import com.success.application.base.BaseRecyclerViewFragment;
import com.success.application.common.param.Params;
import com.success.application.common.result.Result;
import com.success.application.ground.adapter.FriendAdapter;
import com.success.application.ground.adapter.OnlyFriendAdapter;
import com.success.application.ground.bean.Friend;
import com.success.application.myapplication.R;
import com.success.application.widget.SimplexToast;

import java.lang.reflect.Type;

/**
 * Created by Administrator on 2017/10/27.
 */
@SuppressLint("ValidFragment")
public class OnlyFriendFragment extends BaseRecyclerViewFragment<Friend> {

    public static final String BUNDLE_KEY_FRIEND = "BUNDLE_KEY_FRIEND";
    private Friend mfriend;
    //是否只显示朋友圈
    private boolean onlyFriend = true;

    //资讯的pageSize;
    private String pageSize;

    public OnlyFriendFragment(){
        super();
    }

    public OnlyFriendFragment(boolean onlyFriend){
        this();
        this.onlyFriend = onlyFriend;
    }
    @Override
    protected BaseRecyclerViewAdapter<Friend> getRecyclerAdapter() {
        if(onlyFriend){
            return new OnlyFriendAdapter(mContext,BaseRecyclerViewAdapter.ONLY_FOOTER);
        }else {
            return new FriendAdapter(mContext, BaseRecyclerViewAdapter.ONLY_FOOTER);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.ground_friend_fragment_recycler_view;
    }

    @Override
    protected Type getType() {
        return new TypeToken<Result<Friend>>() {
        }.getType();
    }
    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void requestData() {
        try {

            Params P = new Params();
            P.put("count",10);
            P.put("pagenum",mPageNo);
            YuanbaoApi.listDataPyq(P, mHandler);
        } catch (Exception e) {
            SimplexToast.show(mContext,"读取数据失败！");
            e.printStackTrace();
        }
    }

    @Override
    protected void setListData(Result<Friend> result) {
        //覆盖掉父类方法，不继承
        if (isRefreshing) {
            mAdapter.clear();
            //追加第一项，存放滚动图和快捷图标
            if(!onlyFriend){//如果是首页显示，则需要增加一个空行给滚动图和图标使用
                mAdapter.addItem(new Friend());
            }
        } else {
        }
        mAdapter.addAll(result.getRows());
        if (result.getRows() == null || result.getRows().size() < AppConfig.PAGE_SIZE) {
            mAdapter.setState(BaseRecyclerViewAdapter.STATE_NO_MORE,true);
        }else{
            mAdapter.setState(BaseRecyclerViewAdapter.STATE_LOADING,true);
        }
    }

    @Override
    public void onItemClick(int position, long itemId) {
        //第一项是滚动图和快捷图标，屏蔽点击事件
        if(position == 0){
            return;
        }
        super.onItemClick(position, itemId);
        //获得当前点击的资讯实体类
//        Intent intent = new Intent(mContext,NewsDetailActivity.class);
//        mNews = (News) mAdapter.getItem(position);
//        intent.putExtra("NEWS",mNews);
//        startActivity(intent);
    }
}
